package com.example.webwork.controller;

import com.example.webwork.dto.ApiResponse;
import com.example.webwork.entity.User;
import com.example.webwork.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    @Qualifier("UserServiceImpl")
    private IUserService userService;

    @PostMapping("/register")
    @ResponseBody
    public Map<String, Object>  registerUser(@RequestParam("username") String username,
                               @RequestParam("password") String password,
                               @RequestParam("email") String email,
                               @RequestParam("birthday") Date birthday,
                               @RequestParam(value = "avatar", required = false) MultipartFile avatarFile) {

        // 创建 User 对象
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setBirthday(birthday);

        // 调用 UserService 进行用户注册
        boolean isRegistered = userService.registerUser(user, avatarFile);


        Map<String, Object> response = new HashMap<>();
        if (isRegistered) {
            response.put("success", true);
            response.put("message", "User registered successfully");
            return response;
        } else {
            response.put("success", false);
            response.put("message", "用户名已经存在，请重新输入");
            return response; // 返回失败的 JSON 响应
        }
    }

    @PostMapping("/login")
    public User login(@RequestBody User user) {
        System.out.println(userService.login(user.getUsername(), user.getPassword()));
        return userService.login(user.getUsername(), user.getPassword());

    }

    @GetMapping("/getUserByUsername/{username}")
    public User getUserByUsername(@PathVariable String username) {
        System.out.println(username);
        System.out.println(userService.getUserByUsername(username));
        return userService.getUserByUsername(username);
    }

    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }

    @PutMapping("/change")
    @ResponseBody
    public Map<String,Object> updateUser(@RequestBody Map<String, String> request) {
        String username = request.get("username");
        String oldPassword = request.get("oldPassword");
        String newPassword = request.get("newPassword");
        Map<String, Object> response = new HashMap<>();
        System.out.println(username + " " + oldPassword + " " + newPassword);

        boolean isupdated = userService.updateUser(username, oldPassword, newPassword);
        if (isupdated) {
            response.put("success", true);
            response.put("message", "修改密码成功");
            return response;
        } else {
            response.put("success", false);
            response.put("message", "账户名或密码不存在");
            return response; // 返回失败的 JSON 响应
        }


    }

    @DeleteMapping("/{id}")
    public String deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return "User deleted successfully";
    }
}